/*
 * Created on Jun 24, 2005
 */
package org.vectrics.recruiter.job.event;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Set;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.event.Event;
import org.vectrics.event.EventAttachment;
import org.vectrics.event.EventEmailBuilder;
import org.vectrics.event.EventHandler;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.user.Interview;
import org.vectrics.user.PersonSearchCriteria;

/**
 * @author MMoore
 */
public class InterviewEventHandler extends EventHandler {
	private transient Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * Constructor 
	 */
	public InterviewEventHandler() {
		super();
	}
	
	protected EventEmailBuilder getEmailBuilder(Event event, Locale locale) {
		return(new InterviewEmailBuilder(event, this.getInterview(), locale));
	}
	
	protected void doHandleEvent(Event event) {
		log.debug("doHandleEvent(Event event) called");
	}


	protected final Interview getInterview() {
		if (this.getEvent().getAttachments().size() == 0) {
			throw new SystemException("Could not find job attached to job event - event: " + this.getEvent().getDescription());
		}
		Iterator iter = this.getEvent().getAttachments().iterator();
		EventAttachment attachment = (EventAttachment)iter.next();
		Interview interview = RecruitServiceLocator.getRecruitUserService().findInterviewById(new Long(attachment.getObjectId()));
		return(interview);
	}
	
	
	protected Set findNotifyPersons() {
		Set notifyPersons = new HashSet();
		
		Job job = this.getInterview().getJob();
		job = RecruitServiceLocator.getJobProvider().findJob(job.getId());
		if (job != null) {
			PersonSearchCriteria criteria = CoreServiceLocator.createPersonSearchCriteria();
			criteria.addDomain(job.getDomain());
			criteria.addPrivilege("job.edit.notify");
			notifyPersons.addAll(CoreServiceLocator.getPersonService().findPersons(criteria, 0, 1000));
			if (job.getPrimaryRecruiter() != null)
				notifyPersons.add(job.getPrimaryRecruiter());
			if (job.getSecondaryRecruiter() != null)
				notifyPersons.add(job.getSecondaryRecruiter());
			if (job.getHiringManager() != null)
				notifyPersons.add(job.getHiringManager());
		}
		if (this.getInterview().getLeadPerson() != null) {
			notifyPersons.add(this.getInterview().getLeadPerson());
		}
		
		return(notifyPersons);
	}
	

}
